package cn.xrfzh.biz.user.controller;

import cn.xrfzh.api.user.dto.*;
import cn.xrfzh.biz.user.service.IUserService;
import cn.xrfzh.common.annotation.ConfirmExecuteAPI;
import cn.xrfzh.common.annotation.SystemLog;
import cn.xrfzh.common.axios.AxiosResult;
import cn.xrfzh.common.enums.LogType;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author fzh
 * @date 2023/10/24
 * @description 登录控制器
 */
@Api(tags = "user模块")
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class LoginController {

    private final IUserService iUserService;

    /**
     * 用户密码登录
     *
     * @param loginDto
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.LOGIN, description = "用户密码登录")
    @PostMapping("/login")
    public AxiosResult login(@RequestBody @Validated LoginDto loginDto) {
        return iUserService.login(loginDto);
    }

    /**
     * 发送验证码（手机号或者邮箱）
     *
     * @param captchaDto
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.LOGIN, description = "发送验证码（手机号或者邮箱）")
    @PostMapping("/sendCaptcha")
    public AxiosResult sendCaptcha(@RequestBody @Validated CaptchaDto captchaDto) {
        return iUserService.sendCaptcha(captchaDto.getPhoneOrEmail());
    }

    /**
     * 手机验证码登录
     *
     * @param loginByPhoneCaptchaDto
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.LOGIN, description = "手机验证码登录")
    @PostMapping("/loginByPhoneCaptcha")
    public AxiosResult loginByPhoneCaptcha(@RequestBody @Validated LoginByPhoneCaptchaDto loginByPhoneCaptchaDto) {
        return iUserService.loginByPhoneCaptcha(loginByPhoneCaptchaDto);
    }

    /**
     * 邮箱验证码登录
     *
     * @param loginByEmailCaptchaDto
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.LOGIN, description = "邮箱验证码登录")
    @PostMapping("/loginByEmailCaptcha")
    public AxiosResult loginByEmailCaptcha(@RequestBody @Validated LoginByEmailCaptchaDto loginByEmailCaptchaDto) {
        return iUserService.loginByEmailCaptcha(loginByEmailCaptchaDto);
    }

    /**
     * 注销登录
     *
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.LOGOUT, description = "注销登录")
    @GetMapping("/logout")
    public AxiosResult logout() {
        return iUserService.logout();
    }

    /**
     * 忘记密码
     *
     * @param forgetPasswordDto
     * @return
     */
    @ConfirmExecuteAPI
    @SystemLog(module = "用户模块", type = LogType.UPDATE, description = "忘记密码")
    @PostMapping("/forgetPassword")
    public AxiosResult forgetPassword(@RequestBody @Validated ForgetPasswordDto forgetPasswordDto) {
        return iUserService.forgetPassword(forgetPasswordDto);
    }

}
